package user

import (
	"errors"
	config "pd/server/app/pd/config"
	form "pd/server/app/pd/form/user"
	logic "pd/server/app/pd/logic/user"
	model "pd/server/app/pd/model/user"
	"pd/server/com"
	"pd/server/com/errno"
	"pd/server/plugin/api"
	"pd/server/plugin/auth"
	"pd/server/plugin/logger"

	"github.com/gin-gonic/gin"
)

var userLogic logic.UserLogic

// 用户登录
func Login(router *gin.RouterGroup, conf *config.Config) {
	router.POST("/login", func(c *gin.Context) {
		var frm = new(form.LoginForm)
		if err := api.Bind(c, &frm); err != nil {
			return
		}
		if frm.UserName == "" {
			api.No(c, errors.New("用户名不能为空"))
			return
		}

		result, err := userLogic.Login(c, frm)
		if err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		api.Ok(c, result)
	})
}

// 用户登录
func GetUserResources(router *gin.RouterGroup, conf *config.Config) {
	router.GET("/getUserResources", func(c *gin.Context) {
		authInfo := auth.GetAuthInfo(c)
		result, err := userLogic.GetUserResources(c, authInfo.UserId)
		if err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		api.Ok(c, result)
	})
}

// 设置菜单
func SetPower(router *gin.RouterGroup, conf *config.Config) {
	router.POST("/set_power", func(c *gin.Context) {
		var frm = new(model.PowerNode)
		if err := api.Bind(c, &frm); err != nil {
			return
		}

		err := userLogic.SetPower(c, frm)
		if err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		api.Ok(c, nil)
	})
}

// 员工账号列表
func EmployeeList(router *gin.RouterGroup, conf *config.Config) {
	router.POST("/page", func(c *gin.Context) {
		var frm = new(form.EmployeeListForm)
		var err error
		if err = api.Bind(c, &frm); err != nil {
			return
		}
		authInfo := auth.GetAuthInfo(c)
		frm.UserId = authInfo.UserId
		frm.OperatorId = authInfo.OperatorId
		if frm.MerchantId, err = logic.GetUserMerchantId(c, frm.OperatorId); err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		userList, total, err := userLogic.EmployeeList(c, frm)
		if err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		api.Ok(c, gin.H{
			"total":   total,
			"current": frm.Current,
			"size":    frm.Size,
			"records": userList,
		})
	})
}

// 新增员工账号
func AddEmployee(router *gin.RouterGroup, conf *config.Config) {
	router.POST("/add", func(c *gin.Context) {
		var frm = new(form.AddEmployeeForm)
		var err error
		if err = api.Bind(c, &frm); err != nil {
			return
		}
		authInfo := auth.GetAuthInfo(c)
		frm.PaUserId = authInfo.UserId
		if frm.MerchantId, err = logic.GetUserMerchantId(c, authInfo.UserId); err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}
		if !com.IsInt64In(model.RoleIdAdmin, authInfo.MainRoleList) {
			err = errno.ErrPermissionDenied
			api.No(c, err)
			return
		}
		frm.RoleId = model.RoleIdDeliever
		if frm.Status == "" {
			frm.Status = model.StatusOnWork
		}

		err = userLogic.AddEmployee(c, frm)
		if err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		api.Ok(c, frm)
	})
}

func ModifyPassword(router *gin.RouterGroup, conf *config.Config) {
	router.POST("/modifyPassword", func(c *gin.Context) {
		var frm = new(form.ModifyPasswordForm)
		var err error
		if err = api.Bind(c, &frm); err != nil {
			return
		}
		authInfo := auth.GetAuthInfo(c)
		frm.OperatorId = authInfo.OperatorId

		err = userLogic.ModifyPassword(c, frm)
		if err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		api.Ok(c, frm)
	})
}

// 编辑员工账号
func UpdateEmployee(router *gin.RouterGroup, conf *config.Config) {
	router.POST("/edit", func(c *gin.Context) {
		var frm = new(form.AddEmployeeForm)
		var err error
		if err = api.Bind(c, &frm); err != nil {
			return
		}
		authInfo := auth.GetAuthInfo(c)
		frm.PaUserId = authInfo.OperatorId
		if frm.MerchantId, err = logic.GetUserMerchantId(c, authInfo.UserId); err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}
		if !com.IsInt64In(model.RoleIdAdmin, authInfo.MainRoleList) {
			err = errno.ErrPermissionDenied
			api.No(c, err)
			return
		}

		err = userLogic.UpdateEmployee(c, frm)
		if err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		api.Ok(c, frm)
	})
}

// 编辑员工账号
func RemoveEmployee(router *gin.RouterGroup, conf *config.Config) {
	router.POST("/remove", func(c *gin.Context) {
		var frm = new(form.AddEmployeeForm)
		var err error
		if err = api.Bind(c, &frm); err != nil {
			return
		}
		authInfo := auth.GetAuthInfo(c)
		frm.PaUserId = authInfo.OperatorId
		if !com.IsInt64In(model.RoleIdAdmin, authInfo.MainRoleList) {
			err = errno.ErrPermissionDenied
			api.No(c, err)
			return
		}

		err = userLogic.RemoveEmployee(c, frm)
		if err != nil {
			logger.Error(err)
			api.No(c, err)
			return
		}

		api.Ok(c, frm)
	})
}
